﻿using BHan.Context;
using BHan.Model.Entity.Pojo;
using BHan.Repository.IRepository;

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Controls;
using SideMenuItem = HandyControl.Controls.SideMenuItem;
using TabItem = HandyControl.Controls.TabItem;

namespace BHan.VM
{
    /// <summary>
    /// 主窗口的后台代码
    /// </summary>
    public partial class MainWindowVM : ObservableObject,INotifyPropertyChanged
    {

       protected readonly IMenuRepository _menuRepository;
       

        public MainWindowVM(SideMenu sideMenuMain) {
             _menuRepository = App.Current.Services.GetService<IMenuRepository>();
            InitMenuData(sideMenuMain);
            SwitchMenuItemCommand = new RelayCommand<Object>(SwitchMenuItem);
           /* TabItem tabItem = new TabItem();
            tabItem.Header = "首页";
            tabItem.ShowCloseButton = false;
            tabItem.IsSelected = true;
            tabItems.Add(tabItem);*/


        }
       
       
        /// <summary>
        /// 初始化侧边菜单栏数据
        /// </summary>
        /// <param name="sideMenu"></param>
        private void InitMenuData(SideMenu sideMenu)
        {
            var list = _menuRepository.GetNormalMenuList();
            var menus = BuilderMenusData(list);
            menus.ForEach(m =>
            {
                sideMenu.Items.Add(m);
            });
        }
         
        /// <summary>
        /// 递归构造树状侧边菜单栏数据
        /// </summary>
        /// <param name="menus"></param>
        /// <returns></returns>
        private List<SideMenuItem> BuilderMenusData(List<MenuModel> menus)
        {
            List<SideMenuItem> rootMenus = new();

            foreach ( var menu in menus )
            {
                SideMenuItem rootItems = new SideMenuItem();
                rootItems.Header = menu.menuName;
              //  rootItems.Icon = menu.icon;
                rootItems.Tag = menu;
                if( menu.subItems != null && menu.subItems.Count>0)
                {
                    var submenu = BuilderMenusData(menu.subItems);
                    submenu.ForEach(it =>
                    {
                        rootItems.Items.Add(it);
                    });
                     
                }

                rootMenus.Add(rootItems);
            }
            return rootMenus;
        }
 
        /// <summary>
        /// 选中的菜单栏项命令
        /// </summary>
        public RelayCommand<Object> SwitchMenuItemCommand { get; }

        /// <summary>
        /// 获取选中的菜单项
        /// </summary>
        /// <param name="parameter"></param>
        private void SwitchMenuItem(object parameter)
        {
            if (parameter is FunctionEventArgs<Object> eventArgs)
            { 
                SideMenuItem selectedItem = (SideMenuItem)(eventArgs.Info);
                if (selectedItem != null)
                { 
                    AddTabItem(selectedItem);
                }
            }
        }

        [ObservableProperty]
        public ObservableCollection<TabItem> tabItems = new ObservableCollection<TabItem>();
        /// <summary>
        ///  添加 TabItem
        /// </summary>
        private void AddTabItem(SideMenuItem item)
        { 
            var exitTab = tabItems.FirstOrDefault(tab=>tab.Header == item.Header);
            TabItem tabItem = new TabItem();
            tabItem.Header = item.Header;
            tabItem.ShowCloseButton = true;
            MenuModel model = item.Tag as MenuModel;
            if (string.IsNullOrEmpty(model.path))
            {
                tabItem.Content = "待开发";
            }
            else
            {
                var contentPage = App.Current.Services.GetServiceByName<Page>(model.path);
                tabItem.Content = contentPage;
            }
           
            if (exitTab == null) {
                var newTab = tabItem;
                newTab.IsSelected = true;
                tabItems.Add(newTab);
                selectedTab = newTab;
            }
            else
            {
                exitTab.IsSelected = true;
                selectedTab = exitTab;
            }

             
          
        }

        /// <summary>
        /// 选中的 tabItem 项
        /// </summary>
        [ObservableProperty]
        public TabItem selectedTab;



    }

}
